import { create, StateCreator } from 'zustand';
import { devtools } from 'zustand/middleware';

import { IUser } from '@/types/api';
import storage from '@/utils/storage';

interface Create {
    token: string;
    userInfo: IUser.UserItem;
    collapsed: boolean;
    isDark: boolean;
    updateToken: (token: string) => void;
    updateUserInfo: (userInfo: IUser.UserItem) => void;
    updateCollapsed: () => void;
    updateTheme: (isDark: boolean) => void;
}

const useUserStore = create<Create>(
    devtools((set) => ({
        token: '',
        userInfo: {
            createId: 0,
            deptId: '',
            deptName: '',
            job: '',
            mobile: '',
            role: 0,
            roleList: '',
            state: 0,
            userEmail: '',
            userId: '',
            userImg: '',
            userName: ''
        },
        collapsed: false,
        isDark: storage.get('isDark') || false,
        updateToken(token) {
            set({ token });
        },
        updateUserInfo(userInfo) {
            set({ userInfo });
        },
        updateCollapsed() {
            set((state) => {
                return {
                    collapsed: !state.collapsed
                };
            });
        },
        updateTheme: (isDark) => set({ isDark })
    })) as StateCreator<Create, [], []>
);
export default useUserStore;
